package com.wxw.BaseLibrary.tool;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import com.wxw.BaseLibrary.element.Sms;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by david on 2016-01-04.
 */
public class SmsTool {
    public Uri SMS_INBOX = Uri.parse("content://sms/");

    private String key_number = "";
    private String key_text = "";
    private Context context = null;

    public SmsTool(Context context){
        this.context = context;
    }

    public String getKey_number() {
        return key_number;
    }

    public void setKey_number(String key_number) {
        this.key_number = key_number;
    }

    public String getKey_text() {
        return key_text;
    }

    public void setKey_text(String key_text) {
        this.key_text = key_text;
    }

    public List<Sms> getSmsFromPhone() {
        List<Sms> smsList = new ArrayList<Sms>();
        ContentResolver cr = context.getContentResolver();
        String[] projection = new String[] { "_id", "address", "person", "date", "type","body","protocol","read" };//"_id", "address", "person",, "date", "type
        String where = "";
        if (!"".equals(key_number)) {
            where = " address = '"+key_number+"'";
        }
        Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");
        if (null == cur) {
            return null;
        }
        Sms sms = new Sms();
        while ((sms=sms.full(cur))!=null){
            if (!"".equals(key_text) && sms.getBody().indexOf(key_text)>0) {
                smsList.add(sms);
            }
            if ("".equals(key_text)) {
                smsList.add(sms);
            }
        }
        return smsList;
    }
}
